package com.example.jbmsuppliers.sinopec.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.adapter.GuideViewPagerAdapter;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;


/**
 * 李征
 * Created by 家帮忙 on 2017/1/9.
 */

public class GuideActivity extends Activity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    //创建引导页图片

    public static int posin[] = {R.mipmap.dsy_guid1, R.mipmap.dsy_guid2, R.mipmap.dsy_guid3};
    @InjectView(R.id.viewpager)
    ViewPager viewpager;
    @InjectView(R.id.ll)
    LinearLayout ll;

    //设置对应的小图标
    private ImageView points[];
    ImageView iv;
    //记录当前位置
    private int currection;
    //创建装view的集合
    ArrayList<View> views = new ArrayList<>();
    private GuideViewPagerAdapter vpAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
//            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Window window = getWindow();
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(
//                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
//                            | View.SYSTEM_UI_FLAG_FULLSCREEN
//                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);

            setContentView(R.layout.activity_guide);
            ButterKnife.inject(this);
            ExitAppUtlis.getInstance().addActivity(this);
            vpAdapter = new GuideViewPagerAdapter(views);
            getDate();

//        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        if (position == 2)
            ll.setVisibility(View.GONE);
            iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    setGuided();

                }
            });

    }

    @Override
    public void onPageSelected(int position) {
        setCurDot(position);


    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View view) {
        int position = (Integer) view.getTag();
        setCurView(position);
        setCurDot(position);

    }

    /**
     * 初始化数据
     */
    public void getDate() {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        for (int i = 0; i < posin.length; i++) {
            iv = new ImageView(this);
            iv.setLayoutParams(layoutParams);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            iv.setImageResource(posin[i]);
            views.add(iv);
        }
        // 设置数据
        viewpager.setAdapter(vpAdapter);
        // 设置监听
        viewpager.setOnPageChangeListener(this);
        // 初始化底部小点
        initPoint();
    }

    public void initPoint() {
        points = new ImageView[posin.length];
        ll.setVisibility(View.VISIBLE);
        for (int i = 0; i < posin.length; i++) {
            points[i] = (ImageView) ll.getChildAt(i);
            //true默认都为灰色
            points[i].setEnabled(true);
            //设置监听
            points[i].setOnClickListener(this);
            //记录每一个的位置
            points[i].setTag(i);

        }
        //选中的为黑色
        currection = 0;
        points[currection].setEnabled(false);
    }

    //设置当前页面的位置
    public void setCurView(int position) {
        if (position < 0 || position > posin.length) {
            return;
        } else {
            viewpager.setCurrentItem(position);
        }
    }

    //设置小点的位置
    public void setCurDot(int position) {
        if (position < 0 || position > posin.length - 1 || position == currection) {
            return;
        } else {
            points[position].setEnabled(false);
            points[currection].setEnabled(true);
            currection = position;
        }
    }

    private void setGuided() {
        /**
         * 1.当程序重新运行的时候会清空个人微信的姓名
         * 2.来保存boolean值来判断是不是第一次进入
         *
         */
        getSharedPreferences("sinopecall", Activity.MODE_PRIVATE).edit()
                .clear().commit();
        getSharedPreferences("splash", Context.MODE_PRIVATE).edit()
                .putBoolean("isFirstIn", false).commit();
        goHome();//跳转主页面
    }

    private void goHome() { // 跳转

        Intent intent = new Intent(GuideActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }


}
